<template>
  <div>
    <el-form ref="roleForm" :model="roleForm" :rules="rules" label-width="100px">
      <el-form-item label="角色名称" prop="name">
        <el-input v-model="roleForm.name" placeholder="请输入角色名称" />
      </el-form-item>
      <el-form-item label="角色描述" prop="description">
        <el-input v-model="roleForm.description" placeholder="请输入角色描述" />
      </el-form-item>
    </el-form>
    <!-- 底部 -->
    <el-row slot="footer" type="flex" justify="center" style="margin-left:45px">
      <el-col :span="6">
        <el-button size="small" @click="hCancle">取消</el-button>
        <el-button size="small" type="primary" @click="hSubmit">确定</el-button>
      </el-col>
    </el-row>
  </div>
</template>
<script>
import { addRole, updateRole } from '@/api/settings'
export default {
  name: 'AddDialog',
  props: {
    isEdit: {
      type: Boolean,
      require: true
    },
    row: {
      type: Object,
      require: true
    }
  },
  data() {
    return {
      roleForm: {
        name: '',
        description: ''
      },
      rules: {
        name: [{ required: true, message: '角色名称不能为空', trigger: 'blur' }],
        description: [{ required: true, message: '角色描述不能为空', trigger: 'blur' }]
      }
    }
  },
  computed: {
  },
  watch: {
    isEdit: {
      handler(newVal) {
        if (newVal) {
          this.roleForm = this.row
        }
      },
      immediate: true
    }
  },
  created() {
  },
  methods: {
    async doAdd() {
      const res = await addRole(this.roleForm)
      console.log(res, '新增角色成功')
      this.$emit('success')
      this.$message.success('添加成功')
      this.$refs.roleForm.resetFields()
    },
    async doEdit() {
      const res = await updateRole(this.roleForm)
      console.log(res, '编辑角色成功')
      this.$emit('hasEdit')
    },
    hSubmit() {
      this.$refs.roleForm.validate((valid) => {
        if (valid) {
          this.isEdit ? this.doEdit() : this.doAdd()
        }
      })
    },
    hCancle() {
      this.$emit('cancle')
      this.$refs.roleForm.resetFields()
    }
  }
}
</script>
<style lang='less'  scoped>

</style>
